package com.af.security.property;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Map;

/**
 * @comment : 加载security属性， from application.yaml
 * @author : zhenyun.su
 * @since : 2019/5/30
 */

@Configuration
@ConfigurationProperties(prefix = "asu.security")
public class SecurityProperties {
    public static final Logger logger = LoggerFactory.getLogger(SecurityProperties.class);
    private String[] ignoreUrls;
    private String[] permitUrls;
    private String[] authUrls;
    private RememberMe rememberMe;
    private Map<String, Object> loginFailure;
    private Map<String, Object> session;
    private Integer tokenExpired;
    private String loginPage="/login.html";
    private String jwtSigningKey;


    @PostConstruct
    public void print(){
        logger.debug("load security properties, {}", this);
    }
    public String getRememberMeKey(){
        return this.rememberMe.getKey();
    }
    public Integer getRememberMeTokenExpired(){
        return this.rememberMe.getTokenExpired();
    }
    public Integer getLoginFailureMaxAttempt(){
        try{
            return (Integer)loginFailure.get("maxAttempt");
        }catch(Exception e){
            return 8;
        }
    }
    public Long getLoginFailureUnlockUserExpired(){
        try{
            return Long.valueOf(loginFailure.get("unlockUserExpired").toString());
        }catch(Exception e){
            return 10L;
        }
    }
    public Integer getSessionMaxCount(){
        try{
            return (Integer)session.get("maxCount");
        }catch(Exception e){
            return 1;
        }
    }
    public Boolean getSessionPreventLoginPolicy(){
        try{
            return Boolean.valueOf(session.get("preventLoginPolicy").toString());
        }catch(Exception e){
            return false;
        }
    }

    public String[] getPermitUrls() {
        return permitUrls;
    }
    public void setPermitUrls(String[] permitUrls) {
        this.permitUrls = permitUrls;
    }
    public String[] getIgnoreUrls() {
        return ignoreUrls;
    }
    public void setIgnoreUrls(String[] ignoreUrls) {
        this.ignoreUrls = ignoreUrls;
    }
    public String[] getAuthUrls() {
        return authUrls;
    }
    public void setAuthUrls(String[] authUrls) {
        this.authUrls = authUrls;
    }
    public RememberMe getRememberMe() {
        return rememberMe;
    }
    public void setRememberMe(RememberMe rememberMe) {
        this.rememberMe = rememberMe;
    }
    public Map<String, Object> getLoginFailure() {
        return loginFailure;
    }
    public void setLoginFailure(Map<String, Object> loginFailure) {
        this.loginFailure = loginFailure;
    }
    public Map<String, Object> getSession() {
        return session;
    }
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
    public void setTokenExpired(Integer tokenExpired) {
        this.tokenExpired = tokenExpired;
    }
    public Integer getTokenExpired() {
        return tokenExpired;
    }
    public String getLoginPage() {
        return loginPage;
    }
    public void setLoginPage(String loginPage) {
        this.loginPage = loginPage;
    }
    public String getJwtSigningKey() {
        return jwtSigningKey;
    }
    public void setJwtSigningKey(String jwtSigningKey) {
        this.jwtSigningKey = jwtSigningKey;
    }
    @Override
    public String toString() {
        return "SecurityProperties{" +
                "permitUrls=" + Arrays.toString(permitUrls) +
                ", ignoreUrls=" + Arrays.toString(ignoreUrls) +
                ", authUrls=" + Arrays.toString(authUrls) +
                ", rememberMe=" + rememberMe +
                ", loginFailure=" + loginFailure +
                ", tokenExpired=" + tokenExpired +
                ", loginPage='" + loginPage + '\'' +
                ", jwtSigningKey='" + jwtSigningKey + '\'' +
                '}';
    }

    private static class RememberMe {
        private String key;
        private Integer tokenExpired;

        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public Integer getTokenExpired() {
            return tokenExpired;
        }
        public void setTokenExpired(Integer tokenExpired) {
            this.tokenExpired = tokenExpired;
        }
        @Override
        public String toString() {
            return "rememberMe{" +
                    "key='" + key + '\'' +
                    ", tokenExpired=" + tokenExpired +
                    '}';
        }
    }
}
